package com.forum.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forum.enums.myOps1;
import com.forum.pojo.ForumComment;
import com.forum.pojo.LikeRecord;
import com.forum.pojo.vo.ForumCommentVo;
import com.forum.result.Result;
import com.forum.service.ForumCommentService;
import com.forum.service.LikeRecordService;
import com.forum.utils.JwtHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@CrossOrigin
@RequestMapping("/comment")
public class ForumCommentController {
    @Autowired
    private ForumCommentService forumCommentService;
    @Autowired
    private LikeRecordService likeRecordService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 读取评论
     * @param articleId
     * @return
     */
    @GetMapping("/loadComments")
    public Result loadComments(@RequestParam String articleId){
            return Result.ok(forumCommentService.loadComments(articleId));
    }
//    @GetMapping("/loadCommentsByPage/{pageNo}/{pageSize}")
//    public Result loadCommentByPage(@RequestParam String articleId,@PathVariable Integer pageNo,@PathVariable Integer pageSize){
//        return Result.ok(forumCommentService.loadCommentsByPage(articleId,pageNo,pageSize));
//    }
    @ApiOperation("管理员查询评论带分页")
    @GetMapping("/loadCommentsByPage")
    public Result loadCommentsByUserName(@RequestParam(required = false) String username,@RequestParam Integer pageNo,@RequestParam Integer pageSize){
        return Result.ok(forumCommentService.loadCommentsByUserName(username,pageNo,pageSize));
    }
    /**
     * 添加评论
     * @param forumCommentVo
     */
    @PostMapping("/addComment")
    public void addFirstComment(HttpServletRequest request,@RequestBody ForumCommentVo forumCommentVo,@RequestParam(required = false) Long replyUserId){
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        forumCommentService.addComment(forumCommentVo.getReplyUserId(),forumCommentVo,userId);
    }

    /**
     * 评论点赞
     * @param commentId
     * @return
     */

    @RequestMapping(value = {"/doLike"},method = RequestMethod.POST)
    public Result doLike(HttpServletRequest request,@RequestParam Integer commentId) {
        //获取user信息
        String token = request.getHeader("token");
        String value =(String)redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        String commentId1 = String.valueOf(commentId);
        this.likeRecordService.doLike(commentId1, userId,  myOps1.COMMENT_LIKE);
        return Result.ok();
    }
    @ApiOperation("管理员删除评论")
    @DeleteMapping("/deleteByCommentId")
    public Result deleteByArticleId(@RequestParam Integer commentId){
        if(forumCommentService.deleteByArticleId(commentId)){
            return Result.ok();
        }else{
            return Result.build(320,"删除失败");
        }
    }
    @ApiOperation("管理员置顶评论")
    @PostMapping("/topArticle")
    public Result topArticle(@RequestParam Integer commentId,@RequestParam Integer type){
        int result=forumCommentService.setTop(commentId,type);
        if(result==0){
            return Result.build(321,"修改失败");
        }else{
            return Result.ok();
        }
    }
}
